package com.Lixiaoqian.CardPlay.base;

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.annotation.Nullable;
import android.support.v7.app.ActionBar;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.Lixiaoqian.CardPlay.R;
import com.Lixiaoqian.CardPlay.utils.TopSnackbarUtil;
import com.zhy.autolayout.AutoLayoutActivity;

import java.util.ArrayList;

import butterknife.ButterKnife;
import cn.smssdk.EventHandler;


/**
 * Created by HuangZhiBo on 2017/3/27/027.
 */

public abstract class BaseActivity extends AutoLayoutActivity {

    public ActionBar actionBar;
    public Toolbar toolbar;
    private CountDownTimer downTimer;
    public EventHandler eh; //短信验证回调
    public boolean runningDownTimer=false;     //判断是否在倒计时
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        setContentView(getLayoutID());
        ButterKnife.bind(this);
        toolbar= (Toolbar) this.findViewById(R.id.toptoolbar);
        setSupportActionBar(toolbar);
        actionBar = getSupportActionBar();
        initToolbar();
    }

    protected  void initToolbar(){
        actionBar.setDisplayHomeAsUpEnabled(true);
    };

    public void isShowToolbar(boolean is){
        if (is){
            toolbar.setVisibility(View.VISIBLE);
        }else {
            toolbar.setVisibility(View.GONE);
        }

    }

    public abstract int getLayoutID();

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        switch (item.getItemId()){
            case android.R.id.home:
                finish();
        }

        return super.onOptionsItemSelected(item);
    }

    /**
     * 验证码倒计时
     */
    public void CountdownTimer(final TextView tv) {
        downTimer = new CountDownTimer(60 * 1000, 1000) {
            @Override
            public void onTick(long l) {
                runningDownTimer = true;
                tv.setEnabled(false);
                tv.setText((l / 1000) + "秒后重发");
            }

            @Override
            public void onFinish() {
                runningDownTimer = false;
                tv.setText("重新发送");
                tv.setEnabled(true);
            }

        }.start();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
    /**
     * 判断输入框是否为空
     * @param arrayList
     * @return
     */
    public void editEmpty(ArrayList<EditText> arrayList,String info){
        for (EditText editText:arrayList) {
            if (TextUtils.isEmpty(editText.getText())){
                TopSnackbarUtil.showTopBar(info);
                return;
            }
        }
    }
}
